package com.woniu.backstage.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.backstage.common.Result;
import com.woniu.backstage.entity.TbFile;
import com.woniu.backstage.entity.form.FileForm;
import com.woniu.backstage.service.ITbFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 文件表 前端控制器
 * </p>
 *
 * @author spirit
 * @since 2020-04-18
 */
@RestController
@RequestMapping("/backstage/file")
public class TbFileController extends BaseController {
    @Autowired
    private ITbFileService fileService;

    @RequestMapping("/pageLimit")
    public Result pageLimit(Integer page, Integer limit, FileForm fileForm){
        System.out.println("fileForm:"+fileForm);
        Page<TbFile> filePage = new Page<>(page, limit);
        IPage<TbFile> fileIPage = fileService.pageLimit(filePage, fileForm);
        //获取查询的数据
        List<TbFile> files = fileIPage.getRecords();
        System.out.println("files:"+files);
        //获取总的数据条数
        long total = fileIPage.getTotal();
        return Result.success(files, (int) total);
    }

    @RequestMapping("deleteFile")
    public Result deleteFile(Integer id) throws Exception {
        System.out.println("进入更新请求");
        System.out.println("修改的内容："+id);
        int result = fileService.updateFile(id);
        if(result == 1){
            return Result.success();
        }
        throw new Exception("删除失败");
    }

    @RequestMapping("/deleteBatch")
    public Result deleteBatch(Integer[] idArr) throws Exception {
        Integer result = fileService.deleteBatch(Arrays.asList(idArr));
        System.out.println(result);
        if(result > 0){
            return Result.success();
        }
        throw new Exception("删除失败");
    }
}

